package com.ww.springboot.boot.json.valid;

import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jackson.JsonLoader;
import com.fasterxml.jackson.databind.JsonNode;
 
import java.io.IOException;
import java.nio.file.Paths;
 
public class JsonSchemaValidator {
    public static void main(String[] args) {
        JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        try {
            JsonNode schemaNode = JsonLoader.fromFile(Paths.get("D:\\wanwei\\my\\boot-study\\src\\main\\resources\\static\\json\\schema.json").toFile());
            JsonNode jsonNode = JsonLoader.fromFile(Paths.get("D:\\wanwei\\my\\boot-study\\src\\main\\resources\\static\\json\\data.json").toFile());
            JsonSchema schema = factory.getJsonSchema(schemaNode);
            ProcessingReport report = schema.validate(jsonNode);
            if (report.isSuccess()) {
                System.out.println("JSON is valid!");
            } else {
                System.out.println("JSON is not valid!");
                report.forEach(violation -> {
                    System.out.println(violation);
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}